//
//  ViewController.m
//  20-XML-SAX-DOM解析
//
//  Created by 曹曹 on 16/8/10.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"

@interface ViewController () <NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domStuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - XML
// 分为两种解析方式
#pragma mark - 1.SAX 逐行解析 -- 使用系统的解析器 NSXMLParser
- (IBAction)saxParser:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlParser.delegate = self;
    // 运行解析器
    [xmlParser parse];
    
    // 统一资源定位符URL
    // SOAP协议 网络请求协议(JSON以前)
}
// 1.解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 2.节点头 方法的回调
// 记录当前节点名, 方便后续赋值
// 打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"节点头:%@", elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *student = [[Student alloc] init];
        [_stuArray addObject:student];
    }
}
// 3.每个节点的值, 根据节点决定值. 没有值时对应的结果\n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // string是 \n\t
//    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
//        NSLog(@"节点值:%@节点值结束", string);
//        Student *student = _stuArray.lastObject;
//        [student setValue:string forKey:_currentElementName];
//    }
    if (_currentElementName) {
        Student *student = _stuArray.lastObject;
        [student setValue:string forKey:_currentElementName];
    }
}
// 4.节点尾 方法的回调, 判断当前节点的结果
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    _currentElementName = nil;
}
// 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"studentArray:%@", _stuArray);
}
// 解析失败时, 调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败:%@", parseError);
}
#pragma mark - 2.DOM
// 关于GDataXMLNode的引入
// 1.引入.h和.m文件
// 2.引入系统C语言动态库libxml2.tbd
// 3.根据GDataXMLNode头文件中的提示, 添加/user/include/libxml2
// 4.如果是ARC环境, 需要对该文件进行MRC编译, 添加 -fno-objc-arc
// XMLReader 将XML转成NSDictionary
- (IBAction)domParser:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    self.domStuArray = [NSMutableArray array];
    // 1.创建 GDataXMLDocument对象
    // 根据XML的data数据生成一个树形结构的文档
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    // 2.根据document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    // 3.根据根节点获取子节点数组
    NSArray *rootChildElements = rootElement.children;
    // 4.遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElements) {
        // 5.对应所有的student节点
        Student *student = [[Student alloc] init];
        [_domStuArray addObject:student];
        // 6.遍历student节点所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            // 7.给student赋值
            [student setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@", _domStuArray);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
